package org.soarsky.wealthchengdu.module.info;

import java.util.ArrayList;
import java.util.List;

import org.soarsky.wealthchengdu.R;
import org.soarsky.wealthchengdu.entity.Pic;
import org.soarsky.wealthchengdu.imageCache.CacheMemUtil;
import org.soarsky.wealthchengdu.imageCache.ImageLoader;
import org.soarsky.wealthchengdu.imageCache.ImageLoader.ImageCallback;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class MirrordelitAdapter extends BaseAdapter {

	public Bitmap bitmap;
	private List<Pic> tempImages;

	private LayoutInflater mInflater; // 加载容器

	/**
	 * 构造函数
	 * 
	 * @param context
	 * @param bitmaps
	 */
	public MirrordelitAdapter(Context context) {
		this.tempImages = new ArrayList<Pic>();
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	public void setTempList(List<Pic> tempImages) {
		this.tempImages = tempImages;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;

	}

	@Override
	public Object getItem(int item) {
		return item;
	}

	@Override
	public long getItemId(int itemid) {
		return itemid;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final RecentViewHolder holder;

		if (convertView == null) {
			convertView = mInflater.inflate(
					org.soarsky.wealthchengdu.R.layout.gallery_item, null);
			holder = new RecentViewHolder();
			holder.moveableItemimg = (ImageView) convertView
					.findViewById(org.soarsky.wealthchengdu.R.id.showImage);
			convertView.setTag(holder);
		} else {
			holder = (RecentViewHolder) convertView.getTag();
		}
		/**
		 * 回调接口，更新图片
		 */
		if (tempImages != null && tempImages.size() > 0)
			// holder.moveableItemimg.setBackgroundResource(tempImages.get(position%tempImages.size()).getImg());
			loadImage(this.tempImages.get(position % tempImages.size())
					.getImg(), holder.moveableItemimg);
		Log.e("tag", tempImages.size() + "");
		return convertView;
	}

	public class RecentViewHolder {
		public ImageView moveableItemimg;// 图片
	}

	// 这个方法还必须写在adapter里面
	private void loadImage(final String url, final ImageView image) {

		// Bitmap bm = CacheMemUtil.getImage(url);
		Bitmap bm = CacheMemUtil.getImageFromCache(url); // 第一步，仅仅只从内存中读取
		if (bm != null) {
			// 包括从本地获取的，未进行图片处理的图片
			// bm = ImageUtil.zoomBitmap(bm, iconWidth, iconHeight);// 缩放处理
			// bm = ImageUtil.getRoundedCornerBitmap(bm, corners);// 圆角处理
			// CacheMemUtil.putImage2Cache(url, bm);

			image.setImageBitmap(bm);
		} else {
			image.setImageResource(R.drawable.loading);

			ImageLoader.getInstance().loadBitmap(url, new ImageCallback() {

				public void imageLoaded(Bitmap bm) {
					if (bm != null) {
						// //以下两行，根本不该在这里调用，应该在存在本地之前，而不是读取显示时才做处理
						// bm = ImageUtil.zoomBitmap(bm, iconWidth,
						// iconHeight);// 缩放处理
						// bm = ImageUtil.getRoundedCornerBitmap(bm, corners);//
						// 圆角处理
						CacheMemUtil.putImage2Cache(url, bm);
						notifyDataSetChanged();
						// image.setImageBitmap(bm); //不能使用这句方法，否则会在加载完之前错误显示
					}
				}
			});
		}
	}

}
